home *** CD-ROM | disk | FTP | other *** search
/ Nikkei Mac 20 / NIKKEI-MAC-CD-VOL-20-1998-12.ISO.7z / NIKKEI-MAC-CD-VOL-20-1998-12.ISO / オンラインソフト / 9.ウェブ作成ツール / PageSpinner / PageSpinner Docs Japan.sit / PageSpinner Docs Japan / Examples / JavaScript / Dynamic Document Stationery < prev    next >
Text File  |  1996-12-14  |  6KB  |  216 lines

  1. <HTML>
  2. <HEAD>
  3. <TITLE>ダイナミックドキュメント</TITLE>
  4.  
  5. <SCRIPT LANGUAGE="JavaScript">
  6. <!-- Beginning of JavaScript --------
  7. /*
  8.     Dynamic Document
  9.     Written by Jerry Aman, Optima System, Aug 2, 1996. 
  10.     Modified Aug 13, 1996
  11.  
  12.     September 24: Added a tweak that will make the time 
  13.     to be displayed correctly in Navigator 3.0.
  14.     The time seem to be offset by one hour, maybe this have
  15.     something to do with daylight savings?
  16.  
  17.     Part of the PageSpinner distribution.
  18.  
  19.     We will not be held responsible for any unwanted 
  20.     effects due to the usage of this script or any derivative.  
  21.     No warrantees for usability for any specific application are 
  22.     given or implied.
  23.  
  24.     You are free to use and modify this script,
  25.     if the credits above are given in the source code
  26. */
  27.  
  28. // getHourOfDay()
  29. // Returns: Integer containing the hour 0-24
  30.  
  31. function getHourOfDay()
  32. {       var now = new Date();
  33.         return(now.getHours());
  34. }
  35.  
  36.  
  37. // getTime()
  38. // Returns: Text containing the time in the format HH:MM
  39.  
  40. function getTime()
  41. {       
  42.     var now = new Date();
  43.     var minutes = now.getMinutes();
  44.     var divider = ":";
  45.  
  46.     if (minutes<10)
  47.         divider = ":0";
  48.  
  49.         // Hack to get it to display the time 
  50.         // correctly in version 3.0, (adjust for offset)
  51.     if (navigator.appVersion.lastIndexOf('3.') != -1 )
  52.         return( now.getHours()-1 + divider + minutes );
  53.  
  54.         // Other versions may work with this ?    
  55.     return( now.getHours() + divider + minutes );
  56. }
  57.  
  58.  
  59. // isMacintoshBrowser()
  60. // Returns: Boolean value, true if the browser is running under MacOS
  61.  
  62. function isMacintoshBrowser()
  63.     return(navigator.appVersion.lastIndexOf('Mac') != -1 );
  64. }
  65.  
  66.  
  67. // sayHello ()
  68. // Inserts text dynamically in the document when it is called
  69. // Note that all text are dynamically inserted into the document
  70. // when a call to  this function are made in the BODY part of the file.
  71.  
  72. function sayHello ()
  73. {
  74.  
  75.  
  76.     document.write(    "今の時刻は <B>" + 
  77.                 getTime() + 
  78.                 "</B> です。" );
  79.  
  80.  
  81.     if(getHourOfDay()<5 || getHourOfDay()>19)
  82.         document.write('<FONT COLOR="2C396D"> おやすみなさい!</FONT>');
  83.     else 
  84.     {
  85.         if ( getHourOfDay() <11) 
  86.         {
  87.             document.write('<FONT COLOR="52A553">おはようございます</FONT>');
  88.         } 
  89.         else 
  90.         { 
  91.             document.write('<FONT COLOR="ED363C">こんにちわ!</FONT>');
  92.         }
  93.     }
  94. }
  95.  
  96. // -- End of JavaScript code -------------- -->
  97. </SCRIPT>
  98.  
  99. </HEAD>
  100. <BODY BGCOLOR=FFFFFF TEXT=000000>
  101. <H1>JavaScript ダイナミックドキュメント</H1>
  102.  
  103. <B>このひな形ページには、ダイナミックページをを作成する JavaScript の例が含まれています。</B>
  104. <P>
  105. 現在 JavaScript は Netscape Navigator 2.0 以降と MS Internet Explorer 3.0 以降でのみ使用できることに注意して下さい。
  106. <BR>
  107. <FONT COLOR="931B15">あなたはすべての読者が JavaScript を埋め込まれたブラウザを使用していると思ってはいけません。</FONT>
  108. <HR>
  109. この例ではダイナミックページを作るのに JavaScript がどのように使われるかを説明します。ここでは、ブラウザ・システムでのローカル時刻や読者が使っている OS によって表示されるコンテンツが変更されます。
  110. <P>
  111.  
  112. <!-- 1) Let the reader know the browser ID used
  113.       (this is often logged by servers for keeping statistics) -->
  114.  
  115. いらっしゃい <B><SCRIPT>
  116. <!--
  117. document.write(navigator.userAgent)
  118. // -->
  119. </SCRIPT></B> ですね!
  120. <P>
  121.  
  122.  
  123. <!-- 2) Tell the name of the browser used -->
  124.  
  125. <SCRIPT>
  126. <!--
  127. document.write("ここで ", navigator.appName," ", navigator.appVersion, "を使っている方にお会いできるとは")
  128. // -->
  129. </SCRIPT>
  130. <P>
  131.  
  132.  
  133. <!-- 3) Say something that depends upon the readers local time -->
  134. <!-- This is all done in the function sayHello -->
  135.  
  136. <SCRIPT>
  137. <!--
  138. sayHello()
  139. // -->
  140. </SCRIPT>
  141.  
  142.  
  143. <!-- 4) Finally, say something special to the MacOS users -->
  144.  
  145. <SCRIPT>
  146. <!--
  147.  
  148. if (isMacintoshBrowser())
  149.     document.write(
  150.     '<P><FONT COLOR="385FD1">あなたが MacOS を使っているのはとても素敵です!</FONT>'); 
  151. else
  152.     document.write(
  153.     '<P>あなたが Mac を使ってこのページを見ないのはとても悲しいことです。')
  154. // -->
  155. </SCRIPT>
  156.  
  157. <HR>
  158.  
  159. <P>
  160. <B>使い方</B><BR>
  161. 4 つの小さなスクリプトがこのページのテキスト・コンテンツの中に埋め込まれています。このページが読み込まれると、スクリプトが実行され(テキストが挿入)されます。どのような仕組になっているか、ソースファイルを読んでください。すべての関数には説明のコメントが付けてあります。
  162. <P>
  163. スクリプトの中にあるコンテンツをあなたのテキスト関数を呼び出すように置き換えて、スクリプトの <FONT COLOR="FF3366"><CODE>document.write()</CODE></FONT> 関数の中で表示したいテキストを追加してください。複雑なスクリプトの関数を作る場合はそれらを HEAD セクションに置くと良いでしょう。ここで使われている <TT>sayHello</TT> 関数を参考にしてください。
  164.  
  165. <P>
  166. すべてのテキストが <FONT COLOR="FF3366"><CODE>document.write()</CODE></FONT> 関数の中に置かれているかどうか確認してください。そうでない場合は JavaScript をサポートしていないブラウザで見たとき変に表示されてしまいます。
  167.  
  168. <P>
  169. このページを編集するか選択したスクリプトをコピーしてあなたのダイナミックページを作りましょう!
  170. <P>
  171. <HR>
  172. <P>
  173. <B>上で示したダイナミックテキスト例のコード:
  174. </B>
  175. <XMP>いらっしゃい <B><SCRIPT>
  176. <!--
  177. document.write(navigator.userAgent)
  178. // -->
  179. </SCRIPT></B>!</XMP>
  180.  
  181.  JavaScriptをサポートしていないブラウザでは "<B>いらっしゃい</B>" だけが表示されるテキストであることに注意してください。
  182. <P>
  183.  あなたは <FONT COLOR="555555"><!--</FONT> コメント (下記参照)を<I>省略しても</I>スクリプトを書くことができます。しかしここでそうすると実際のスクリプトが JavaScript をサポートしていないブラウザで見えてしまいます。あまりお薦めできません:
  184.  
  185. <FONT COLOR="FF3366"><XMP>いらっしゃい <B><SCRIPT>document.write(navigator.userAgent);</SCRIPT></B>!
  186. </XMP></FONT>
  187.  
  188. <P>
  189. <HR>
  190. <P>
  191. <B>この関数は HEAD に置かれて、ブラウザが Mac である場合に true を返します:</B><BR>
  192.  
  193. <XMP>function isMacintoshBrowser()
  194.   return(navigator.appVersion.lastIndexOf('Mac') != -1 );
  195. }
  196. </XMP>
  197.  
  198. この関数は、スクリプトの中で以下のように使います:
  199. <XMP>if (isMacintoshBrowser())
  200.   document.write('Mac specific text...'); 
  201. else
  202.   document.write('Text for other platforms...')
  203. </XMP>
  204. <P>
  205. <HR>
  206. <P>
  207. <B>バグについて:</B><BR>
  208. スクリプトのこのバージョンでは、 Navigator 3.0 (および 2.0) で時刻が正しく表示されるよう getTime() を変更しました。 2.0 と比較すると 3.0 バージョンでは 時刻が 1 時間進んでいるようです。これは夏時間か何かなのかなあ?
  209.  
  210. <P>
  211. <!--Translated by <A HREF="mailto:hosoka@ca2.so-net.or.jp">Shuji HOSOKAWA</A>-->
  212. </BODY>
  213. </HTML>
  214.